package it.polimi.meteocal.ejb.usermanagement;

import it.polimi.meteocal.ejb.invitationmanagement.Invitation;
import it.polimi.meteocal.ejb.usermanagement.dto.UserDTO;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * Entity implementation class for Entity: UserEntity
 *
 */
@Entity
@Table(name="USERS")
@NamedQueries({
	@NamedQuery(name=User.findAll,
				query="SELECT u FROM User u ORDER BY u.registeredOn ASC"),
	@NamedQuery(name=User.FIND,
				query="SELECT u FROM User u WHERE u.email = :email")
})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static final String findAll = "User.findAll";
	public static final String FIND = "User.FIND";
	 
	@Id	
	private String email;
	
    private String name;
      
    private String surname;
	
	private String password; //sha-256 + hex
	
	private String address;
	
	private String phone;
	
	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date birthdate;
	
	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date registeredOn;
	
	@ElementCollection(targetClass = Group.class)
    @CollectionTable(name = "USERS_GROUPS",
                    joinColumns = @JoinColumn(name = "email"))
    @Enumerated(EnumType.STRING)
    @Column(name="groupname")
    private List<Group> groups;
	
	/*@ManyToMany
	  @JoinTable(
	      name="User_Invitation",
	      joinColumns={@JoinColumn(name="USER_EMAIL", referencedColumnName="email")},
	      inverseJoinColumns={@JoinColumn(name="INV_ID", referencedColumnName="invitationid")})
	  private List<Invitation> invitations;
	 */

	public User() {
		super();
	}
	
	public User(UserDTO user){
         
        this.email        = user.getEmail();
        this.name    = user.getName();
        this.surname     = user.getSurname();        
        this.password     = DigestUtils.sha256Hex(user.getPassword());
        this.registeredOn = new Date();
        this.address	  = user.getAddress();
        this.phone		  = user.getPhone();
        this.birthdate    = user.getBirthdate();
    }
	
	
 
    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public Date getBirthdate() {
		return birthdate;
	}

	public void setBirthdate(Date birthdate) {
		this.birthdate = birthdate;
	}

	public String getEmail() {
        return email;
    }
  
    public void setEmail(String email) {
        this.email = email;
    }
  
    /**
     * @return the password in SHA256 HEX representation
     */
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public Date getRegisteredOn() {
        return registeredOn;
    }
 
    public void setRegisteredOn(Date registeredOn) {
        this.registeredOn = registeredOn;
    }
 
    public List<Group> getGroups() {
        return groups;
    }
 
    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }
 
    public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public static String getFindAll() {
		return findAll;
	}

	@Override
    public String toString() {
        return "User [email=" + email + ", name=" + name
                + ", surname=" + surname + ", password=" + password
                + ", registeredOn=" + registeredOn + ", groups=" + groups + "]";
    }
	
}
